define(function (require) {
var zsApp = require('app');    
zsApp.service('perSelectCache', ['$rootScope', '$http', 'promptCache', function($rootScope, $http, promptCache) {
    
    var choseIdArr=[];                                 // 定义数组用于存放前端显示
    var idStr='';

    var choseNameArr=[];
    var nameStr='';

    var flag='';                                     // 是否点击了全选，是为yes
    var isSelectAll = false;                         // 默认未选中

    // 初始化变量
    this.init = function() {
        choseIdArr=[];                                
        idStr='';
        choseNameArr=[];
        nameStr='';

        flag='';                                    
        isSelectAll = false;                       
    };

    this.defSelect = function(fnId, defaultUrl) {

        var TreeReqVo = { 
            id          : fnId, 
            pageNumber  : 1,
            pageSize    : 20,
        };

        $http
        ({
            url     : $rootScope.zsUrl+'digitalhome-boss/'+defaultUrl,
            method  : 'POST',
            data    : TreeReqVo,

        }).success(function(res) {  console.log(res.result);

            angular.forEach(res.result, function(obj, i) {

                 choseIdArr[i]   = obj.operationId;
                 choseNameArr[i] = obj.operationName;

            });

        }).error(function(err) {
            console.log(err);
        });
    };

    // 全选
    this.selectAll = function (perBool, objId, objName) { 

        if( perBool == true ) { 

            isSelectAll = true;
            choseIdArr   = angular.copy(objId);
            choseNameArr = angular.copy(objName);

            flag = 'yes';
        }else {

            isSelectAll = false;
            choseIdArr   = [];
            choseNameArr = [];
            flag = 'no';
        }

    };

    // 多选
    this.select = function (selectId, selectName, bool, len) { 

        if(flag == 'yes') {                             // 在全选的基础上操作
                            
            idStr   = choseIdArr.join(',') + ',';
            nameStr = choseNameArr.join(',') + ',';

        }
        if(bool == true) {        

            choseIdArr.push(selectId);
            choseNameArr.push(selectName);

        }
        else {

            choseArr(choseIdArr, selectId);
            choseArr(choseNameArr, selectName);
            
        }

    };

    function choseArr(arr, val) {
        for( var i=0; i<arr.length; i++ ) {
            if( arr[i] == val ) {
                arr.splice(i, 1);
            }
        }
    };

    this.isPermission = function(whichFnId, whichFnName, fnId, fnName, whichPerId, whichPerName, whichUrl) {

        var perId = [];
        var perName = [];

        if( choseIdArr.length == 0 ) {
            perId.push('00');
            perName.push('00');
        }else {
             perId = [];
             perName = [];
            for(var i=0;i<choseIdArr.length;i++) { 
                perId.push(choseIdArr[i]);
                perName.push(choseNameArr[i]);
            };

        }

        var permissionInfo = { [whichFnId] : fnId, [whichFnName] : fnName, [whichPerId] : perId, [whichPerName] : perName };

        $http
        ({
            url     : $rootScope.zsUrl+'digitalhome-boss/'+whichUrl,
            method  : 'POST',
            data    : permissionInfo,

        }).success(function(res) {

            if( res.result_code != '0000' ) {
                promptCache.errorPrompt('关联权限失败！');
                return false;
            }else {
                promptCache.successPrompt('关联权限成功！');
            }

        }).error(function(err) {

            promptCache.errorPrompt('关联权限失败！');

        });

        // empty array
        choseIdArr=[];                            
        idStr='';
        choseNameArr=[];
        nameStr='';
    };

}]);
});